#include "SceneEngine.h"
#include "Engine.h"

SceneEngine* SceneEngine::sceneEngine;
extern Sprite * s_sprite[SPRITE_LIST::SPRITE_MAX];
extern Sprite * s_font[FONT_LIST::FONT_MAX];
extern int s_language;
extern char s_text[TEXT::TEXT_MAX][LANGUAGE::MAX][256];

SceneEngine::SceneEngine()
{
	sceneEngine = NULL;
	sceneLevel = NULL;	
	countFPS = 0;
	currentFPS = 0;
	FPSLastTime = 0;
}


SceneEngine::~SceneEngine()
{
}

SceneEngine* SceneEngine::GetSceneEngine()
{
	if ( sceneEngine == NULL )
		sceneEngine = new SceneEngine();
	return sceneEngine;
}

//open scene
void SceneEngine::OpenScene ( int sceneID )
{
	sceneLevel[sceneID].isOpen = true;
}

//close scene
void SceneEngine::CloseScene ( int sceneID )
{
	sceneLevel[sceneID].isOpen = false;
}

//load levels into sceneLevel
void SceneEngine::LoadScene ( char *guiPackName )
{
	char line[50], temp[10];
	int param[VGPConfig::MAX_PARAM_TEMPLATE], levelID = -1, elementCount = 0; 

	FileSystem *fileSystem = FileSystem::GetFileSystem();
	FILE* f = fileSystem->Open ( guiPackName, "r");

	if ( f == NULL )
	{
		LOGE ( "\n Can not load GUI: %s", guiPackName );
		return;
	}

	this->sceneLevel = new SceneLevel[GUI_LAYER::MAX_LAYER];

	while ( !feof ( f ) )
	{
		//read line
		fgets ( line , sizeof ( line ), f );
		//check to find levelID
		if ( line[0] == 'E') //element
		{
			levelID++;
			//read number of element for the levelID
			sscanf ( &line[0], "%s" "%d", &temp, &sceneLevel[levelID].numElement );

			sceneLevel[levelID].elememt = new int[sceneLevel[levelID].numElement * VGPConfig::MAX_PARAM_TEMPLATE];			
			elementCount = 0;
		}
		else //read each element of levelID
		{
			sscanf ( &line[0], "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d",					
					&sceneLevel[levelID].elememt[elementCount],
					&sceneLevel[levelID].elememt[elementCount + 1],
					&sceneLevel[levelID].elememt[elementCount + 2],
					&sceneLevel[levelID].elememt[elementCount + 3],
					&sceneLevel[levelID].elememt[elementCount + 4],
					&sceneLevel[levelID].elememt[elementCount + 5],
					&sceneLevel[levelID].elememt[elementCount + 6],
					&sceneLevel[levelID].elememt[elementCount + 7],
					&sceneLevel[levelID].elememt[elementCount + 8],
					&sceneLevel[levelID].elememt[elementCount + 9],
					&sceneLevel[levelID].elememt[elementCount + 10],
					&sceneLevel[levelID].elememt[elementCount + 11] );			
			elementCount += VGPConfig::MAX_PARAM_TEMPLATE;	
		}

		memset ( line, 0, sizeof ( line ) );
	}

	/*
	LOGE( "\n\n-------------------------------\n" );
	int i =0, j, k;
	for ( i = 0; i < GUI_LAYER::MAX_LAYER; i++ )
	{
		for ( j = 0; j < sceneLevel[i].numElement; j++ )
		{
			for ( k = 0; k < VGPConfig::MAX_PARAM_TEMPLATE; k ++ )
				LOGE ( "%5d" , sceneLevel[i].elememt[j*VGPConfig::MAX_PARAM_TEMPLATE + k] );
			LOGE( "\n\n");
		}
		LOGE( "\n\n");
	}
	*/
}

//get properties
void SceneEngine::GetSceneElementProperties ( int sceneID, int elementID )
{
	sceneEngine_template		= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_TEMPLATE];
	sceneEngine_x				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_X];
	sceneEngine_y				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_Y];
	switch ( sceneEngine_template )
	{
	case GUI_TEMPLATE::SPRITEBASED:
		sceneEngine_visible			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_VISIBLE];
		sceneEngine_spriteID		= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SPRITE_ID];
		sceneEngine_frameID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_FRAME_ID];		
		sceneEngine_animID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_ANIM_ID];
		sceneEngine_animCount		= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_ANIM_COUNT];
		sceneEngine_scale			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCALE];
		break;

	case GUI_TEMPLATE::SELECTRECT:
	case GUI_TEMPLATE::SCROLLLIST:
		sceneEngine_w				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_W];
		sceneEngine_h				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_H];
		break;	

	case GUI_TEMPLATE::STRINGBASED:
		sceneEngine_visible			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_VISIBLE];
		sceneEngine_fontID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_FONT_ID];
		sceneEngine_textID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_TEXT_ID];
		sceneEngine_anchor			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_VALIGN] | sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_HALIGN];
		break;

	case GUI_TEMPLATE::BUTTON:
		sceneEngine_visible			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_VISIBLE];
		sceneEngine_spriteID		= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SPRITE_ID];
		sceneEngine_frameID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_FRAME_ID];
		sceneEngine_framePressID	= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_FRAME_PRESSED];
		sceneEngine_animID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_ANIM_ID];
		sceneEngine_animCount		= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_ANIM_COUNT];
		sceneEngine_scale			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_BUTTON_SCALE];
		sceneEngine_tapable			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_TAPABLE];
		sceneEngine_focus			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_FOCUS];
		sceneEngine_pressed			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_PRESSED];		
		break;
	
	case GUI_TEMPLATE::SCROLLINGTEXT:
		sceneEngine_w				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_W];
		sceneEngine_h				= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_H];
		sceneEngine_visible			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_VISIBLE];
		sceneEngine_fontID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCROLL_TEXT_FONT_ID];
		sceneEngine_textID			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCROLL_TEXT_TEXT_ID];
		sceneEngine_anchor			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCROLL_TEXT_VALIGN] | sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCROLL_TEXT_HALIGN];
		sceneEngine_speed			= sceneLevel[sceneID].elememt[elementID * VGPConfig::MAX_PARAM_TEMPLATE + PARAM_SCROLL_TEXT_SPEED];
		break;
	}

}

//paint scene detail
void SceneEngine::PaintScene ( int sceneID, int _x, int _y )
{
	if ( sceneLevel[sceneID].isOpen )
	{		

		for ( int i = 0; i < sceneLevel[sceneID].numElement; i++ )
		{
			GetSceneElementProperties ( sceneID, i );
			switch ( sceneEngine_template )
			{
			case GUI_TEMPLATE::SPRITEBASED:
				if ( sceneEngine_visible )
				{
					if ( sceneEngine_frameID != -1 )
						s_sprite[sceneEngine_spriteID]->PaintFrame ( sceneEngine_frameID, sceneEngine_x, sceneEngine_y, sceneEngine_scale );
					else
						LOGE ("\nPaint anim not available " );
				}
				break;

			case GUI_TEMPLATE::SELECTRECT:

				break;

			case GUI_TEMPLATE::SELECTPOINT:

				break;

			case GUI_TEMPLATE::STRINGBASED:
				if ( sceneEngine_visible )
				{
					s_font[sceneEngine_fontID]->DrawString ( s_text[sceneEngine_textID][s_language], sceneEngine_x, sceneEngine_y, sceneEngine_anchor );
				}
				break;

			case GUI_TEMPLATE::BUTTON:
				if ( sceneEngine_visible )
				{
					if ( sceneEngine_focus || sceneEngine_pressed )
						s_sprite[sceneEngine_spriteID]->PaintFrame ( sceneEngine_framePressID, sceneEngine_x, sceneEngine_y, sceneEngine_scale );
					else
						s_sprite[sceneEngine_spriteID]->PaintFrame ( sceneEngine_frameID, sceneEngine_x, sceneEngine_y, sceneEngine_scale );
				}
				break;

			case GUI_TEMPLATE::SCROLLLIST:

				break;

			case GUI_TEMPLATE::SCROLLINGTEXT:

				break;
			}
		}
	}

}


void SceneEngine::PaintFPS ()
{
	long time = clock();
	countFPS++;

	if ( time - FPSLastTime >= 1000 )
	{
		currentFPS = countFPS;
		countFPS = 0;
		FPSLastTime = time;
	}
	char* strFPS = new char [ strlen ( "FPS: ") + strlen (  VGPLib::GetVGPLib()->ConvertNumberToString( currentFPS ) ) ];
	strcpy ( strFPS, "FPS: " );
	strcat ( strFPS, VGPLib::GetVGPLib()->ConvertNumberToString( currentFPS ) );	
	s_font[FONT_LIST::FONT_MEDIUM]->DrawString ( strFPS, HALF_SCREEN_W , SCREEN_H - 10, VGPLib::TOP | VGPLib::CENTER );
}